package com.heima.controller;

import com.heima.annotation.OperationFill;
import com.heima.dto.RoleDto;
import com.heima.pojo.Role;
import com.heima.result.Result;
import com.heima.service.RoleService;
import com.heima.vo.PageResult;
import com.heima.vo.RoleVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/system/role")
@Api(tags = "角色列表模块")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 查询角色列表
     */
    @GetMapping("/list")
    @OperationFill
    public PageResult list(RoleDto roleDto,
                           @RequestParam(value = "params[beginCreateTime]",required = false) String begin,
                           @RequestParam(value = "params[endCreateTime]",required = false) String end){
        roleDto.setBeginTime(begin);
        roleDto.setEndTime(end);
        log.info("查询角色列表:{}",roleDto);
        return roleService.list(roleDto);
    }


    /**
     * 批量删除角色
     */
    @DeleteMapping("/{ids}")
    @OperationFill
    public Result delete(@PathVariable("ids") List<Integer> ids){
        log.info("批量删除角色:{}",ids);
        roleService.delete(ids);
        return Result.success();
    }

    /**
     * 根据角色编号查询信息
     */
    @GetMapping("/{roleId}")
    @OperationFill
    public Result<RoleVo> getById(@PathVariable("roleId") Integer roleId){
        log.info("根据角色编号查询信息:{}",roleId);
        RoleVo rolevo =roleService.getById(roleId);
        return Result.success(rolevo);
    }

    /**
     * 修改角色
     */
    @PutMapping
    @OperationFill
    public Result updateRole(@RequestBody Role role){
        log.info("修改角色:{}",role);
        roleService.updateRole(role);
        return Result.success();
    }

    /**
     * 修改角色数据权限
     */
    @PutMapping("/dataScope")
    @OperationFill
    public Result updateDataScope(String dataScope, Integer roleId){
        log.info("修改角色数据权限:{}, {}", dataScope, roleId);
        roleService.updateDataScope(dataScope, roleId);
        return Result.success();
    }

    /**
     * 新增角色
     */
    @PostMapping
    @OperationFill
    public Result addCourse(@RequestBody RoleVo roleVo){
        log.info("新增角色:{}",roleVo);
        roleService.addRole(roleVo);
        return Result.success();
    }

    /**
     * 修改角色状态
     */
    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody Role role){
        log.info("修改角色状态:{}",role);
        roleService.updateStatus(role);
        return Result.success();
    }


}
